package net;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client extends JFrame implements ActionListener {
	JTextArea jta;
	JScrollPane jsp;
	JTextField jtf;
	JButton jb;
	JPanel jp1;
	PrintStream ps;

	public Client() {
		jta = new JTextArea();
		jsp = new JScrollPane(jta);
		jtf = new JTextField(10);
		jb = new JButton("发送");
		jb.addActionListener(this);
		jp1 = new JPanel();
		jp1.add(jtf);
		jp1.add(jb);
		this.add(jsp, "Center");
		this.add(jp1, "South");
		this.setSize(300, 200);
		this.setTitle("qq聊天-客户端");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		try {
			Socket s = new Socket("127.0.0.1", 9999);
			BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			ps = new PrintStream(s.getOutputStream(), true);
			while (true) {
				String str = br.readLine();
				jta.append("服务器对客户端说：" + str + "\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Client cl = new Client();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jb) {
			String s = jtf.getText();
			jta.append("客户端对服务器说：" + s + "\r\n");
			ps.println(s);
			jtf.setText("");
		}

	}
}
